import net.sf.cglib.proxy.Enhancer;
import org.junit.Test;

import java.lang.reflect.Proxy;

/**
 * @author wsz
 * @date 2021/10/13 18:16
 **/
public class ProxyTest {

    /**
     * jdbc代理需要实现接口
     */
    @Test
    public void jdbcProxy() {
        UserService object = new UserServiceImpl();

        Class<?>[] interfaces = object.getClass().getInterfaces();
        System.out.println(interfaces.length);
        UserService u1 =  (UserService) Proxy.newProxyInstance(this.getClass().getClassLoader(), object.getClass().getInterfaces(), (proxy, method, args) -> {
            System.out.println("before");
            Object result = method.invoke(object, args);
            System.out.println("after");
            return result;
        });

        System.out.println(u1.getAll());
    }


    @Test
    public void cglibProxy() {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(CommonClass.class);
        enhancer.setCallback(new CommonInterceptor());

        Class<?>[] interfaces = CommonClass.class.getInterfaces();
        System.out.println(interfaces.length);
        CommonClass commonClass = (CommonClass) enhancer.create();
        commonClass.setName("11");
        System.out.println(commonClass);
    }

}
